// adapter/rest/BookController.java
package com.library.bookservice.adapter.rest;

import com.library.bookservice.application.service.BookService;
import com.library.bookservice.domain.model.Book;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书REST控制器
 * 处理图书相关的HTTP请求
 */
@RestController
@RequestMapping("/api/v1/books")
public class BookController {

    private final BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @PostMapping
    public ResponseEntity<BookResponse> createBook(@Valid @RequestBody CreateBookRequest request) {
        Book book = new Book(
                request.isbn(),
                request.title(),
                request.author(),
                request.genre(),
                request.publicationDate(),
                request.totalCopies()
        );
        Book savedBook = bookService.createBook(book);
        BookResponse response = BookResponse.from(savedBook);
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    @GetMapping("/{id}")
    public ResponseEntity<BookResponse> getBookById(@PathVariable Long id) {
        Book book = bookService.getBookById(id);
        BookResponse response = BookResponse.from(book);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/isbn/{isbn}")
    public ResponseEntity<BookResponse> getBookByIsbn(@PathVariable String isbn) {
        Book book = bookService.getBookByIsbn(isbn);
        BookResponse response = BookResponse.from(book);
        return ResponseEntity.ok(response);
    }

    @GetMapping
    public ResponseEntity<List<BookResponse>> getAllBooks() {
        List<Book> books = bookService.getAllBooks();
        List<BookResponse> responses = books.stream()
                .map(BookResponse::from)
                .toList();
        return ResponseEntity.ok(responses);
    }

    @GetMapping("/author/{author}")
    public ResponseEntity<List<BookResponse>> getBooksByAuthor(@PathVariable String author) {
        List<Book> books = bookService.getBooksByAuthor(author);
        List<BookResponse> responses = books.stream()
                .map(BookResponse::from)
                .toList();
        return ResponseEntity.ok(responses);
    }

    @GetMapping("/genre/{genre}")
    public ResponseEntity<List<BookResponse>> getBooksByGenre(@PathVariable String genre) {
        List<Book> books = bookService.getBooksByGenre(genre);
        List<BookResponse> responses = books.stream()
                .map(BookResponse::from)
                .toList();
        return ResponseEntity.ok(responses);
    }

    @PostMapping("/{id}/borrow")
    public ResponseEntity<BookResponse> borrowBook(@PathVariable Long id) {
        Book book = bookService.borrowBook(id);
        BookResponse response = BookResponse.from(book);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/{id}/return")
    public ResponseEntity<BookResponse> returnBook(@PathVariable Long id) {
        Book book = bookService.returnBook(id);
        BookResponse response = BookResponse.from(book);
        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBook(@PathVariable Long id) {
        bookService.deleteBook(id);
        return ResponseEntity.noContent().build();
    }
}
